#include "files.h"

#define FILEMODE 0666

_FILE* _fopen(const char* filename, const char* flag)
{
  int fd = -1;
  std::string sflag(flag);
  if (sflag == "w")
  {
    fd = open(filename, O_EXCL | O_CREAT |O_WRONLY, FILEMODE); 
  }
  else if (sflag == "r") 
  {
    fd = open(filename, O_EXCL | O_CREAT | O_RDONLY, FILEMODE); 
  }
  else if (sflag == "a")
  {
    fd = open(filename, O_APPEND); 
  }
  else 
  {
    return nullptr;
  }
  if (fd == -1)
  {
    return nullptr;
  }

  _FILE* t = new _FILE();
  t->fileno = fd;

 return t;
}

  

